package simpledb.tx.recovery;

import simpledb.log.BasicLogRecord;

/**
 * The ROLLBACK log record.
 * 
 * @author Edward Sciore
 */
class RollbackRecord implements LogRecord {
  private int txnum;

  /**
   * Creates a new rollback log record for the specified transaction.<br>
   * 为指定的事务创建新的回滚日志记录.
   * 
   * @param txnum the ID of the specified transaction
   */
  public RollbackRecord(int txnum) {
    this.txnum = txnum;
  }

  /**
   * Creates a log record by reading one other value from the log. <br>
   * 通过从日志中读取其他日志的一个其他值来创建日志记录
   * 
   * @param rec the basic log record
   */
  public RollbackRecord(BasicLogRecord rec) {
    txnum = rec.nextInt();
  }

  /**
   * Writes a rollback record to the log. This log record contains the ROLLBACK
   * operator, followed by the transaction id. <br>
   * 将回滚记录写入到日志，这个日志记录包含了ROLLBACK操作,之后会返回事务id.
   * 
   * @return the LSN of the last log value 最后一个日志值的LSN
   */
  public int writeToLog() {
    Object[] rec = new Object[] { ROLLBACK, txnum };
    return logMgr.append(rec);
  }

  public int op() {
    return ROLLBACK;
  }

  public int txNumber() {
    return txnum;
  }

  /**
   * Does nothing, because a rollback record contains no undo information. <br>
   * 什么都不做，因为回滚记录不包含撤销信息。
   */
  public void undo(int txnum) {
  }

  public String toString() {
    return "<ROLLBACK " + txnum + ">";
  }
}
